<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

/**
 * build a component with initial property values
 */
function build_component($component_type, $property_set, $container, $semf_root, $page_doc)
{
	$from_plugin = 1;
	
	//the following parameters are used insdie build_component_xml
	$component_type = $component_type;
	
	$component_id = generate_new_id($page_doc);
	$component_container = $container;
	
	//this will generate the component..
	include($semf_root."page_designer/build_component_xml.php");
	
	//print_r($property_set);
	
	$property_groups = array_keys($property_set);
	foreach($property_groups as $property_group)
	{
		$properties = array_keys($property_set[$property_group]);
		foreach($properties as $property)
		{
			$property_value = $property_set[$property_group][$property];
			
			//the following parameters are used inside 
			$property_group = $property_group;
			$key = $property;
			$value = $property_value;
			
			
			//to change the above property with value..
			include($semf_root."page_designer/change_property.php");
			
			//echo "|<br/>";
		}
	}
	
	
	$component_ele = $page_doc->getElementById($component_id);
	
	$child_containers = array();
	$container_ids = array();
	$child_nodes = $component_ele->childNodes;
	
	foreach($child_nodes as $child_node)
	{
		$tag_name = $child_node->tagName;
		if($tag_name != "container")
		{
			continue;
		}
		$id = $child_node->getAttribute("id");
		$x = $child_node->getAttribute("x");
		$y = $child_node->getAttribute("y");
		$child_containers[$id] = array('x'=> $x, 'y' => $y);
		
		$next_i = 0;
		for($i = 0; $i < count($container_ids); $i ++)
		{
			$the_container_id = $container_ids[$i];
			$the_x = $child_containers[$the_container_id]['x'];
			$the_y = $child_containers[$the_container_id]['y'];
			
			
			if($the_y > $y)
			{
				$next_i = $i;
				break; // current y is lesser than the ith y
			}
			elseif($the_y == $y) //now check x..
			{
				if($the_x > $x)
				{
					$next_i = $i;
					break;
				}
			}
		}
		$next_i = $i;
		
		//now $next_i contain the next place to insert
		//now shift array from i to end..
		for($i = count($container_ids); $i > $next_i; $i --)
		{
			$container_ids[$i] = $container_ids[$i-1];
		}
		// now $next_id is free to have this id..
		$container_ids[$next_i] = $id;
	}
	 
	return array(
				"component"=> $component_id,
				"containers"=> $container_ids,
				"doc"=> $page_doc);
}



/**
 * generate a new id
 */
function generate_new_id($doc)
{
	$ele = NULL;
	$id = NULL;
	do
	{
		$id = rand();
		$ele = $doc->getElementById("comp".$id);
	}
	while($ele != NULL);
	return "comp".$id;
}

function copy_node($src, $target_parent, $target_doc)
{
	if($src->nodeType == XML_TEXT_NODE) 
	{ //for text node
		$node_text = $src->nodeValue;
		$new_text_element = $target_doc->createTextNode($node_text);
		$target_parent-> appendChild($new_text_element);
	}
	else if($src->nodeType == XML_ELEMENT_NODE) // for element node
	{
		
		$nodeName = $src->nodeName;
		
		$new_element = $target_doc->createElement($nodeName);
		$target_parent->appendChild($new_element);
		
		// add attributes
		foreach($src->attributes as $attri)
		{
			$key = $attri->name;
			$value = $attri->value;
			$new_element->setAttribute($key, $value);
		}
	
		foreach ($src->childNodes as $child)
		{
			copy_node($child, $new_element, $target_doc);
		}		
	}
}

/**
 * add 0/1  0 - to set
 			1- to add
 */
function change_component_property($component_type, $property_set, 
				$component_id, $change_string, $semf_root, $page_doc, $add)
{

	$from_plugin = 1;
	
	
	$comp_element = $page_doc->getElementById($component_id);
	// just readd properties..
	//if($add)
	{
		$property_groups = array_keys($property_set);
		$found_group = false;
		foreach($property_groups as $property_group)
		{
			if($found_group) break;
			$comp_property_group_elements = $comp_element-> childNodes;
			foreach($comp_property_group_elements as $comp_property_group_element)
			{
				$tagName = $comp_property_group_element->tagName;
				if($tagName != "property")
				{
					continue;
				}
				$key = $comp_property_group_element->getAttribute("key");
				
				if($key == $property_group)
				{
					$found_group = true;
					$properties = array_keys($property_set[$property_group]);
					$found_property = false;
					foreach($properties as $property)
					{
						if($found_property) break;
						$comp_property_elements = $comp_property_group_element-> childNodes;
						foreach($comp_property_elements as $comp_property_element)
						{
							$tagName = $comp_property_element->tagName;
							if($tagName != "property")
							{
								continue;
							}
							$key = $comp_property_element->getAttribute("key");
							if($key == $property)
							{
								$found_property = true;
								$value = $property_set[$property_group][$property];
								$current_value = "";
								if($comp_property_element->firstChild != NULL && 
									$comp_property_element->firstChild->nodeType == XML_TEXT_NODE)
								{
									$current_value = $comp_property_element->firstChild->nodeValue;
								}
								
								$pos = false;
								if(isset($change_string) && $change_string != "")
								{
									$prefix = $postfix = "";
									$prefix = "/******************************************\n".
												" * ".$change_string." *\n".
												" ****************************************/\n";
									$postfix= "/******************************************\n".
												" * END OF ".$change_string." *\n".
												" ****************************************/\n";
									$pos = strpos($current_value, $change_string);
									if($pos !== false)
									{
										$start_index = strpos($current_value, $prefix);
										$end_index = strpos($current_value, $postfix) + strlen($postfix);
										
										$early_current_value = substr($current_value, 0, $start_index);
										$post_current_value = substr($current_value, $end_index);
										$current_value = $early_current_value.$post_current_value;
									}
									
								}
								$pos = false;
								if($pos === false || $add != 1) //well edit only this happens
								{
									if(isset($change_string) && $change_string!= "")
									{
									}
									if($add != 1)
									{
										$current_value = "";
									}
									$value = $prefix.$value.$postfix.$current_value;
									if($comp_property_element->firstChild != NULL)
									{
										$comp_property_element->firstChild->nodeValue = $value;
									}
									else
									{
										$property_text_ele = $page_doc->createTextNode($value);
										$comp_property_element->appendChild($property_text_ele);
									}
								}
								else
								{
									echo "<i>The plugin is already applied for the current file.<i><br/>\n";
								}
								break;
							}
						}
					}
					if($found_property === false)
					{
						//just create a new element
						$properties = array_keys($property_set[$property_group]);
						$found_property = false;
						foreach($properties as $property)
						{
							$comp_property_element = $page_doc->createElement("property");
							$comp_property_element->setAttribute("key", $property);
							$comp_property_text = $page_doc->createTextNode($property_set[$property_group][$property]);
							
							$comp_property_element->appendChild($comp_property_text);
							
							$comp_property_group_element->appendChild($comp_property_element);
							
						}
					}
					break;
				}
			}
		}
	}
	return array("doc"=> $page_doc);
}

function change_page_property($property_set, $change_string, $semf_root, $page_doc, $add)
{
	$component_type = "Page";
	
	$root = $page_doc->documentElement;
	$root_childs = $root->childNodes;
	foreach($root_childs as $root_child)
	{
		if($root_child->getAttribute("type") != "Page")
		{
			continue;
		}
		$component_id = $root_child->getAttribute("id");
		$ret_val = change_component_property($component_type, $property_set, 
				$component_id, $change_string, $semf_root, $page_doc, $add);
		$page_doc = $ret_val["doc"];
	}
	return array("doc"=> $page_doc);
}
?>